package uy.com.nfc.localassigner.managers;

import java.lang.reflect.Type;
import java.util.HashMap;

import uy.com.nfc.localassigner.MainActivity;
import android.app.Activity;
import android.content.SharedPreferences;

import com.example.nfctagcommons.classes.VigilancePoint;
import com.example.nfctagcommons.exceptions.ConnectivityException;
import com.example.nfctagcommons.server.ServerConnector;
import com.example.nfctagcommons.utils.SharedPreferencesUtils;
import com.example.nfctagcommons.utils.StringPool;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class SessionManager {
	private static final Gson gson = new Gson();

	public static String getGuardId(Activity activity) {
		SharedPreferences sharedPreferences = SharedPreferencesUtils.getSharedPreferences(activity.getApplicationContext(), MainActivity.class);
		String guardId = sharedPreferences.getString(SharedPreferencesUtils.LOGIN, StringPool.BLANK);
		return guardId;
	}

	public static void setGuardId(Activity activity, String guardId) {
		SharedPreferences sharedPreferences = SharedPreferencesUtils.getSharedPreferences(activity.getApplicationContext(), MainActivity.class);
		SharedPreferencesUtils.storePreference(activity, SharedPreferencesUtils.LOGIN, guardId, sharedPreferences);
	}
	
	public static boolean isLoggedIn(Activity activity){
		SharedPreferences sharedPreferences = SharedPreferencesUtils.getSharedPreferences(activity.getApplicationContext(), MainActivity.class);
		String guardId = sharedPreferences.getString(SharedPreferencesUtils.LOGIN, StringPool.BLANK);
		return !guardId.equals(StringPool.BLANK);
	}

	public static HashMap<String, VigilancePoint> getMyAssignedPoints(Activity activity) {
		SharedPreferences sharedPreferences = SharedPreferencesUtils.getSharedPreferences(activity.getApplicationContext(), MainActivity.class);
		String myAssignedLocalsJSON = sharedPreferences.getString(SharedPreferencesUtils.MY_ASSIGNED_LOCALS, StringPool.BLANK);
		try{
        	Type t = new TypeToken<HashMap<String, VigilancePoint>>(){}.getType();
        	HashMap<String, VigilancePoint> myAssignedLocals = gson.fromJson(myAssignedLocalsJSON, t);
	        return myAssignedLocals;
        }catch (Exception x){
        	x.printStackTrace();
        	return null;
        }
	}

	public static void setMyAssignedPoints(Activity activity, HashMap<String, VigilancePoint> myAssignedLocals) {
		String myAssignedLocalsJSON = gson.toJson(myAssignedLocals);
		SharedPreferences sharedPreferences = SharedPreferencesUtils.getSharedPreferences(activity.getApplicationContext(), MainActivity.class);
		SharedPreferencesUtils.storePreference(activity, SharedPreferencesUtils.MY_ASSIGNED_LOCALS, myAssignedLocalsJSON, sharedPreferences);
	}

	public static String getLoggedGuardName(Activity activity) {
		SharedPreferences sharedPreferences = SharedPreferencesUtils.getSharedPreferences(activity.getApplicationContext(), MainActivity.class);
		String guardName = sharedPreferences.getString(SharedPreferencesUtils.GUARD_NAME, StringPool.BLANK);
		return guardName;
	}

	public static void setLoggedGuardName(Activity activity, String guardName) {
		SharedPreferences sharedPreferences = SharedPreferencesUtils.getSharedPreferences(activity.getApplicationContext(), MainActivity.class);
		SharedPreferencesUtils.storePreference(activity, SharedPreferencesUtils.GUARD_NAME, guardName, sharedPreferences);
	}

	public static void loadMyPoints(Activity activity) {
		try {
			setMyAssignedPoints(activity, ServerConnector.getVigilanceSet(activity, getGuardId(activity)));
		} catch (ConnectivityException e) {
			e.printStackTrace();
		}
	}

}
